package cn.zymall.app.service.impl;

import cn.zymall.app.mapper.AddressMapper;
import cn.zymall.app.service.AddressService;
import cn.zymall.app.service.CityViewService;
import cn.zymall.common.model.ShippingAddress;
import cn.zymall.common.utils.response.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xuxianda
 * @Description: TODO(这里用一句话描述作用)
 * @date 2020/8/27 15:24
 * @Copyright
 * @return
 **/
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    CityViewService cityViewService;

    @Override
    public HttpResult select(ShippingAddress shippingAddress) {
        try {

            List<ShippingAddress> select = addressMapper.select(shippingAddress);
            return new HttpResult().success("查询成功", select);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult().fail("系统内部错误");
        }
    }

    @Override
    public HttpResult remove(ShippingAddress shippingAddress) {
        try {
            shippingAddress.setShippingStatus(-1);
            if (addressMapper.updateById(shippingAddress) > 0) {
                return new HttpResult().success("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult().fail("删除失败，请重试");
        }
        return new HttpResult().fail("删除失败，请重试");
    }

    @Override
    public HttpResult update(ShippingAddress shippingAddress) {
        try {
            if (addressMapper.updateById(shippingAddress) > 0) {
                return new HttpResult().success("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult().fail("修改失败，请重试");
        }
        return new HttpResult().fail("修改失败，请重试");
    }

    @Override
    public HttpResult insert(ShippingAddress shippingAddress) {
        try {

            if (addressMapper.insert(shippingAddress) > 0) {
                return new HttpResult().success("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult().fail("添加失败，请重试");
        }
        return new HttpResult().fail("添加失败，请重试");
    }

}
